package com.bw.device.view

import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmActivity
import com.bw.device.R
import com.bw.device.adapter.DeviceAdapter
import com.bw.device.databinding.ActivityDeviceBinding
import com.bw.device.entity.DeviceEntity
import com.bw.device.net.DeviceViewModel
/**
 *  智慧园区-01 -设备管理 -v1.1-张梦停
 *  2025/05/10
 */
@Route(path = RoutePath.PATH_DEVICE)
class DeviceActivity : BaseMvvmActivity<ActivityDeviceBinding,DeviceViewModel>() {

    private lateinit var dAdapter:DeviceAdapter
    private val originNewList = mutableListOf<DeviceEntity>() // 原始数据

    override fun initView() {
        binding.topBar.tvTitle.text = "设备管理"
        binding.topBar.ivBack.setOnClickListener { finish() }
        binding.topBar.ivMore.visibility = View.VISIBLE
        binding.topBar.ivMore.setOnClickListener {
            ARouter.getInstance().build(RoutePath.PATH_ADD_DEVICE).navigation()
        }
        dAdapter = DeviceAdapter()
        binding.rvDevice.adapter = dAdapter
        binding.rvDevice.layoutManager = LinearLayoutManager(this)
        dAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(this)
                .setNegativeButton("取消",null)
                .setMessage("是否删除")
                .setPositiveButton("确定",DialogInterface.OnClickListener { dialogInterface, i ->
                    vm.delDevice(dAdapter.data[position].id)
                    dAdapter.removeAt(position)
                    dAdapter.notifyItemRemoved(position)
                })
                .show()
            true
        }
        binding.etSearch.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {}
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
            override fun afterTextChanged(s: Editable?) {
                val query = s.toString()
                val filteredList = if (query.isEmpty()) {
                    originNewList
                } else {
                    originNewList.filter {
                        it.deviceName.contains(query, ignoreCase = true)
                    }
                }
                dAdapter.data.clear()
                dAdapter.data.addAll(filteredList)
                dAdapter.notifyDataSetChanged()
            }
        })
    }

    override fun initData() {
        vm.deviceSuccess.observe(this){
            originNewList.clear()
            originNewList.addAll(it)
            dAdapter.data.addAll(originNewList)
            dAdapter.notifyDataSetChanged()
        }
        vm.deleteDeviceSuccess.observe(this){
            ToastUtils.showLong("删除成功")
        }
    }


    override fun onResume() {
        super.onResume()
        vm.getDevice()
    }

}